﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KTOP.Homeworks.Hw4.Logics.DijkstraRoutes
{
    internal class RouteLabel
    {
        private static readonly RouteLabel INFINITE = new RouteLabel {IsInfinite = true};

        public bool IsConstant { get;  set; }
        public bool IsInfinite { get;  set; }
        public int RouteValue { get;  set; }
        
        public RouteLabel Copy()
        {
            return new RouteLabel()
                       {
                           IsConstant = IsConstant,
                           IsInfinite = IsInfinite,
                           RouteValue = RouteValue
                       };
        }

        public static RouteLabel Infinite
        {
            get { return INFINITE; }
        }

        public static RouteLabel Value(int val)
        {
            return new RouteLabel {RouteValue = val};
        }

        public static RouteLabel Constantly(int val)
        {
            return new RouteLabel {IsConstant = true, RouteValue = val};
        }

        #region Equality

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (RouteLabel)) return false;
            return Equals((RouteLabel) obj);
        }

        public bool Equals(RouteLabel other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.IsConstant.Equals(IsConstant) && other.IsInfinite.Equals(IsInfinite) && other.RouteValue == RouteValue;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = IsConstant.GetHashCode();
                result = (result * 397) ^ IsInfinite.GetHashCode();
                result = (result * 397) ^ RouteValue;
                return result;
            }
        }

        #endregion

        public override string ToString()
        {
            return IsConstant ? RouteValue + "+" : IsInfinite ? "-" : RouteValue + "";
        }
            
        public static bool operator >(RouteLabel label, int i)
        {
            if (label.IsInfinite) return true;
            return label.RouteValue > i;
        }

        public static bool operator <(RouteLabel label, int i)
        {
            if (label.IsInfinite) return false;
            return label.RouteValue < i;
        }
    }
}
